using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace sb.core {
    public class RewriteContext {
        private string _InitialUrl;
        private NameValueCollection _Params;

        public static RewriteContext Current {
            get {
                if (HttpContext.Current.Items.Contains("RewriteContextInfo")) {
                    return (RewriteContext)HttpContext.Current.Items["RewriteContextInfo"];
                } else return new RewriteContext();
            }
        }
        public NameValueCollection Params {
            get { return _Params; }
            set { _Params = value; }
        }
        public string InitialUrl {
            get { return _InitialUrl; }
            set { _InitialUrl = value; }
        }

        public RewriteContext() {
            _Params = new NameValueCollection();
            _InitialUrl = String.Empty;
        }
        public RewriteContext(NameValueCollection param, string url) {
            _InitialUrl = url;
            _Params = new NameValueCollection(param);
        }
    }

    public class RewriteModuleSectionHandler : IConfigurationSectionHandler {
        private XmlNode _XmlSection;
        private string _RewriteBase;
        private bool _RewriteOn;

        public XmlNode XmlSection {
            get { return _XmlSection; }
        }

        public string RewriteBase {
            get { return _RewriteBase; }
        }
        public bool RewriteOn {
            get { return _RewriteOn; }
        }
        public object Create(object parent, object configContext, System.Xml.XmlNode section) {
            _RewriteBase = HttpContext.Current.Request.ApplicationPath + "/";

            try {
                _XmlSection = section;
                _RewriteOn = Convert.ToBoolean(section.SelectSingleNode("rewriteOn").InnerText);
            } catch (Exception ex) {
                throw (new Exception("Error while processing RewriteModule configuration section.", ex));
            }
            return this;
        }
    }
}
